var app = getApp();
Page({
    data:{
        curPage : "/zofui_sales/kfc/index",
        siteroot : app.siteInfo.siteroot.replace('app/index.php', ''),uniacid: app.siteInfo.uniacid,
        params : {},
        sort : [],
		inited : false,
        userinfo : {},
        options : {},
        anima : false,
        itemStyle : '',
        itemHeight : '',
        list : [],
        timer : false,
    },
    onLoad:function(options){
        var self = this;
        var zfid = options.zfid ? options.zfid : (options.scene ? options.scene : 0);
        self.data.options = options;
        
        app.com.auth(function(user){
            var postdata = {
                op : 'info',
                id : options.id,
                zfid : zfid,
            };
            app.com.http('kfc','POST',postdata,0,false,'','',function(res){
                if(res.data && !res.data.errno){

                    var data = res.data.data;
                    self.setData({
                       sharedata : res.data.data.sharedata,
                       params : res.data.data.params,
                       userinfo : res.data.data.userinfo,
                       list : res.data.data.list ? res.data.data.list : [],
                       inited : true,
                    },function(){
                        self.startani();
                    });
                    
                    wx.setNavigationBarTitle({
                        title:  res.data.data.params.title ? res.data.data.params.title : 'kfc',
                    });

                    if(res.data.data.params.topcolor){
                        wx.setNavigationBarColor({
                            frontColor: '#ffffff',
                            backgroundColor: res.data.data.params.topcolor,
                        });
                    }
                }else{
                    app.com.alert(res.data.message);
                }
            });
        });
    },
    onShow(){
        this.startani();
    },
    onHide(){
        clearInterval(this.data.timer);
    },
    onShareAppMessage : function(){
        var self = this;
        return {
            title: self.data.sharedata.title,
            path: self.data.sharedata.path,
            imageUrl : self.data.sharedata.img,
        }
    },
    onPullDownRefresh: function () {
        this.onLoad(this.data.options);
        wx.stopPullDownRefresh();
    },
    getPhoneNumber( e ){
        var self = this;
        if( e.detail.errMsg == 'getPhoneNumber:ok') {
            var postdata = {
                iv: e.detail.iv,
                encryptedData: e.detail.encryptedData,
                op : 'encrypt',
            };
            app.com.http('user_v1','POST',postdata,0,true,'','',function(res){
                if (res.data && !res.data.errno) {
                    self.setData({
                        'userinfo.tel' : 1,
                    });
                    self.toorder();
                }else{
                    app.com.alert(res.data.message);
                }
            });
        }else{
            app.com.alert('请允许授权'+e.detail.errMsg);
        }
    },
    share(){
        this.selectComponent('#poster').onClose();
    },
    toorder(){
        var self = this;
        if(self.data.params.isneedauth*1 == 1){
            self.loginauth = this.selectComponent('#loginauth');
            self.loginauth.hideauth();
            return false;
        }
        if(self.data.params.submess && wx.requestSubscribeMessage){
            wx.requestSubscribeMessage({
                tmplIds : self.data.params.submess,
                complete : function(){
                    self.toorderPost();
                }
            })
        }else{
            self.toorderPost();
        }
    },
    authed(e){
        var self = this;
        self.setData({
            'params.isneedauth' : 0,
        })
        self.toorderPost();
    },
    toorderPost(){
        var self = this;
        if(self.data.params.kfxcx*1 == 1){
            var postdata = {
                op : 'toorder',
            };
            app.com.http('kfc','POST',postdata,0,true,'','',function(res){
                if (res.data && !res.data.errno) {
                    var e = {
                        currentTarget : {
                            dataset : {
                                weburl : res.data.data.url,
                            }
                        }
                    };
                    self.toweburl(e);
                }else{
                    app.com.alert(res.data.message);
                }
            });
        }else{
            self.selectComponent('#xcxmess').show();
        }
    },
    startani(){
        var self = this;
        clearInterval(self.data.timer);
        if(self.data.list.length > 0){
            self.getHeight();
            self.data.timer = setInterval(function() {
                self.animate();
            },2000);
        }
    },
    getHeight: function() {
        var self = this;
        if(self.data.inited){
            var q = self.createSelectorQuery();
            q.select("#item0").boundingClientRect(function(e) {
                self.setData({
                    itemHeight: e.height
                });
            });
            q.exec();
        }
    },
    animate: function() {
        var t = this;
        this.setData({
            itemStyle: "transform:translateY(-" + this.data.itemHeight + "px)"
        }), this.setData({
            anima: !this.data.anima
        }), setTimeout(function() {
            t.data.list.push(t.data.list[0]), t.data.list.shift(), t.setData({
                list: t.data.list,
                anima: !t.data.anima
            });
        }, 500);
    },
    location : function( e ){
        app.com.location( e );
    },
    otherapp : function( e ){
        app.com.otherapp( e );
    },
    navigateto : function( e ){
        app.com.navigateto( e );
    },
    footNavigate(e){
        var url = e.currentTarget.dataset.url;
        wx.switchTab({
          url: url
        })
    },
    redirectto : function( e ){
        app.com.redirectto( e );
    },    
    callphone : function(e){
        app.com.callphone( e );
    },
    toweburl : function(e){
        app.com.toweburl( e );
    },
    showimages : function( e ){
        app.com.showimages( e );
    }
    

})